const storageGet = (key, type = 'sessionStorage') => {
    try {
        return JSON.parse(window[type].getItem(key));
    } catch (e) {
        console.log(e);
    }
};
const storageSet = (key, val, type = 'sessionStorage') => window[type].setItem(key, JSON.stringify(val));
const storageRemove = (key, type = 'sessionStorage') => window[type].removeItem(key);
const cookieGet = (key) => {
    let arr,
        reg = new RegExp('(^| )' + key + '=([^;]*)(;|$)');
    if ((arr = document.cookie.match(reg))) return arr[2];
    else return null;
};
const cookieDelete = (cname, cvalue, exdays) => {
    let d = new Date();
    d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
    let expires = 'expires=' + d.toUTCString();
    document.cookie = cname + '=' + cvalue + '; ' + expires;
};
const getQueryVariable = (variable) => {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return false;
};
// 设置cookie
const cookieset = function (key, value, day) {
    document.cookie = `${key}=${value};Max-age=${day * 24 * 60 * 60}; path=/`;
};
// 获取cookie
const cookieget = function (key) {
    let res = null;
    let all = document.cookie;
    all = all.split(';');
    all.forEach((item) => {
        if (item.split('=')[0].trim() == key) {
            res = item.split('=')[1].trim();
        }
    });
    return res;
};
export const getSession = (key) => storageGet(key);
export const setSession = (key, val) => storageSet(key, val);
export const removeSession = (key) => storageRemove(key);
export const getLocal = (key) => storageGet(key, 'localStorage');
export const setLocal = (key, val) => storageSet(key, val, 'localStorage');
export const removeLocal = (key) => storageRemove(key, 'localStorage');
export const getCookie = (key) => cookieGet(key);
export const deleteCookie = (cname, cvalue, exdays) => cookieDelete(cname, cvalue, exdays);
export const getQueryParams = (key) => getQueryVariable(key);
export const setcookie = (key, value, day) => cookieset(key, value, day);
export const getcookie = (key) => cookieget(key);
